using System;
using System.Data;
using System.Xml;
using System.Xml.XPath;
using System.Collections;

namespace Umbraco.Ecommerce
{
	public class Cart
	{
		private ArrayList _Data = new ArrayList();

		public void AddItemToCart(CartItem c) 
		{
			bool hasProduct = false;
		    foreach (CartItem c1 in _Data) 
				if (c1.ProductId == c.ProductId) 
					{
						hasProduct = true;
						if ((c1.Quantity + c.Quantity)>0) c1.Quantity += c.Quantity;
						else RemoveItem(c);
					}
		
			if (!hasProduct && c.Quantity > 0) 
				_Data.Add(c);
		}

		public decimal TotalSum
		{
			get 
			{
				Decimal d = 0;
				foreach (CartItem c in _Data) 
					d += c.Price * c.Quantity;
				return d;
			}
		}

		public void ClearItems() {_Data.Clear();}

		public void RemoveItem(CartItem c) 
		{
			for (int i = _Data.Count -1;i>=0;i--)
				if (((CartItem)_Data[i]).ProductId == c.ProductId) 
						_Data.Remove(_Data[i]);
		}

		public void SetQuantityOnItem(string productId, int Quantity) {
			foreach (CartItem ci in _Data) {
				if (ci.ProductId == productId)
					ci.Quantity = Quantity;
			}
		}

		public void RemoveItem(string VariantId) 
		{
			for (int i = _Data.Count -1;i>=0;i--)
				if (((CartItem)_Data[i]).ProductId == VariantId) 
					_Data.Remove(_Data[i]);
		}		

		public CartItem[] Items {
			get {
				CartItem[] retval = new CartItem[_Data.Count];
				for (int i = 0; i < _Data.Count;i++) 
				{
					retval[i] = (CartItem)_Data[i];
				}
				return retval;
			}
		}



		public DataTable ToDataTable() {
			DataTable dt = new DataTable("item");
			dt.Columns.Add("id");
			dt.Columns.Add("productId");
			dt.Columns.Add("productName");
			dt.Columns.Add("quantity");
			dt.Columns.Add("price");
			dt.Columns.Add("sum");
			dt.Columns.Add("url");
			
			foreach (CartItem c in _Data)	
			{
				DataRow dr = dt.NewRow();
				dr["productId"] = c.ProductId;
				dr["productName"] = c.ProductName;
				dr["quantity"] = c.Quantity;
				dr["price"] = c.Price.ToString("c",BusinessLogic.Store.NumberFormatInfo);
				dr["sum"] = ((decimal)(c.Quantity * c.Price)).ToString("c",BusinessLogic.Store.NumberFormatInfo);
				dr["url"] = c.Url;
				dt.Rows.Add(dr);
			}
			return dt;
		}
		
		/*
		 * <cart total="decimal" totalDisplay="string (currency)">
		 * <item>
		 * <productId>string</productId>
		 * <productName>string</productName>
		 * <quantity>int32</quantity>
		 * <price>currency string</price>
		 * <sum>currency string</sum>
		 * <url>string</url>
		 * </item>
		 * <item>
		 * ...
		 * </item>
		 * </cart>
		*/ 
		public XPathNodeIterator ToXml()  
		{ 
				DataTable dt = new DataTable("item");
				dt.Columns.Add("productId");
				dt.Columns.Add("productName");
				dt.Columns.Add("quantity");
				dt.Columns.Add("price");
				dt.Columns.Add("sum");
				dt.Columns.Add("url");
				
				foreach (CartItem c in _Data)	
				{
					DataRow dr = dt.NewRow();
					dr["productId"] = c.ProductId;
					dr["productName"] = c.ProductName;
					dr["quantity"] = c.Quantity;
					dr["price"] = c.Price.ToString("c",BusinessLogic.Store.NumberFormatInfo);
					dr["sum"] = ((decimal)(c.Quantity * c.Price)).ToString("c",BusinessLogic.Store.NumberFormatInfo);
					dr["url"] = c.Url;
					dt.Rows.Add(dr);
				}

				DataSet ds = new DataSet("cart");
				ds.EnforceConstraints = false;
				ds.Tables.Add(dt);
				XmlDataDocument xdoc = new XmlDataDocument(ds);
				XmlNode at = xdoc.CreateElement("total");
				at.InnerText = this.TotalSum.ToString();
				xdoc.DocumentElement.AppendChild(at);
				at = xdoc.CreateElement("totalDisplay");	
				at.InnerText = this.TotalSum.ToString("c",BusinessLogic.Store.NumberFormatInfo);
				xdoc.DocumentElement.AppendChild(at);

				at = xdoc.CreateElement("itemcount");	
				at.InnerText = Items.Length.ToString();
				xdoc.DocumentElement.AppendChild(at);
				XPathNodeIterator xp = xdoc.CreateNavigator().Select(".");
				return xp;
		}
	}

	public class CartItem
    {
		public string ProductId;
		public string ProductName;
		public decimal Price;
		public int Quantity;
		public string Url;
	}
}